//
//  RechargeMainTableViewController.m
//  Fucker
//
//  Created by Coby on 17/9/4.
//  Copyright © 2017年 梦拓科技. All rights reserved.
//

#import "RechargeMainTableViewController.h"
#import "TaskWallSystem.h"
#import "ViewController.h"
#import "UIImageView+WebCache.h"


@interface RechargeMainTableViewController ()
@property(nonatomic,copy)NSDictionary *data;
@end

@implementation RechargeMainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"rechargeItem" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"rechargeItem"];
}
-(void)viewWillAppear:(BOOL)animated{
    [self UrlRequesetGetChargeList];
    [super viewWillAppear:animated];
}
-(void)UrlRequesetGetChargeList{
    NSLog(@"StartGetChargeList");
    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithContentsOfFile:UserDeviceIDFile];
    if (muDict == nil) {
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"未查询到deviceId，请先运行脚本！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:cancelAction];
        [self presentViewController:alertControl animated:YES completion:nil];
    }else {
        
        __weak RechargeMainTableViewController *weakSelf = self;
        dispatch_async(dispatch_queue_create("URLSuccess", NULL), ^{
            [[TaskWallSystem sharedInstance] requestServiceList:^(void) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    __weak RechargeMainTableViewController *strongSelf = weakSelf;
//                    strongSelf.data = [[TaskWallSystem sharedInstance] chargeQuestList];
                    strongSelf.data = [NSDictionary dictionaryWithDictionary:[[TaskWallSystem sharedInstance] chargeQuestList]];
                    [self.tableView reloadData];
                });
            }];
        });
    }
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary * TaskDictionary=[_data[@"data"] objectAtIndex:indexPath.row];
    if ([[TaskWallSystem sharedInstance] SelectedTaskFromList]==nil){
        NSLog(@"设置选中的任务");
        [[TaskWallSystem sharedInstance] setSelectedTaskFromList:TaskDictionary];
        [TaskDictionary writeToFile:SelectedTaskFile  atomically:NO];
        [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:[NSString stringWithFormat:@"已选择的任务%@",TaskDictionary[@"GameInfo"][@"GameName"]]];
        
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }else{
        [self doAlertSelected];
    }
    return;
}

- (void)doAlertSelected{
    // 准备初始化配置参数
    NSString *title = @"警告";
    NSString *message = @"请先停止任务并点击垃圾桶删除当前选择任务";
    NSString *okButtonTitle = @"OK";
    UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    // 创建操作
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alertDialog addAction:okAction];
    [self presentViewController:alertDialog animated:YES completion:nil];
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
   // [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(_data == nil){
        return 1;
    }else{
        if(_data[@"data"] != nil){
            return ((NSArray *)_data[@"data"]).count;
        }
        
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *defaultCell=[tableView dequeueReusableCellWithIdentifier:@"rechargeItem"];
    if (!defaultCell) {
        defaultCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rechargeItem"];
    }
    if(_data==nil){
        UILabel *label=(UILabel *)[defaultCell viewWithTag:1];
        label.text=[NSString stringWithFormat:@"没有请求到数据列表，请检查设置是否正确"];
        return defaultCell;
    }else{
        if(_data[@"data"]!=nil){
            NSLog(@"Now make %ld cell code:%ld",indexPath.row,[_data[@"code"] integerValue]);
            NSMutableDictionary * CellArray=[_data[@"data"] objectAtIndex:indexPath.row];
            
            UILabel *label=(UILabel *)[defaultCell viewWithTag:1];
            label.text=[NSString stringWithFormat:@"%@",CellArray[@"GameInfo"][@"GameName"]];
            NSLog(@"CellArray %@",CellArray);
            
            UILabel *labelRemainAmount=(UILabel *)[defaultCell viewWithTag:3];
            labelRemainAmount.text=[NSString stringWithFormat: @"余量:%@",CellArray[@"ServiceCurrentHourLeft"]];
            
            UILabel *typeLabel = (UILabel *)[defaultCell viewWithTag:4];
            
            switch ([CellArray[@"ServiceTaskType"] intValue]) {
                case 1:
                    
                    typeLabel.text = @"新增";
                    
                    break;
                    
                case 2:
                    
                    typeLabel.text = @"留存";
                    
                    break;
                    
                case 3:
                    
                    typeLabel.text = @"充值";
                    
                    break;
                    
                case 4:
                    
                    typeLabel.text = @"新增重做";
                    
                    break;
                    
                case 5:
                    
                    typeLabel.text = @"留存重做";
                    
                    break;
                case 6:
                    
                    typeLabel.text = @"新增练级";
                    
                    break;
                default:
                    break;
            }
            
            
            UIImageView *urlImage =(UIImageView *)[defaultCell viewWithTag:2];
            [urlImage sd_setImageWithURL:[NSURL URLWithString:CellArray[@"GameInfo"][@"GameIcon"]]];
            return defaultCell;
        }
    }
    UILabel *label=(UILabel *)[defaultCell viewWithTag:1];
    label.text=[NSString stringWithFormat:@"labke%ld",indexPath.row];
    return defaultCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 62;
}
@end
